<template>
	<view>

		<uni-popup ref="add_role_item" :mask-click="false" >
			<add-role-item :sid="sid" @close="()=>$refs.add_role_item.close()" 
			@refreshData="()=>{loadData();$refs.add_role_item.close();}"></add-role-item>
		</uni-popup>

		<uni-popup ref="edit_role_item" :mask-click="false" >
			<edit-role-item v-if="showEditRoleItem" @close="()=>{$refs.edit_role_item.close();showEditRoleItem=false}" @refreshDataRoleItem="()=>{loadData();$refs.edit_role_item.close()}" :sid="sid" :id="idEdit"></edit-role-item>
		</uni-popup>

		<view class="uni-container">
			<a-row>
				<a-button @click="()=>{$refs.add_role_item.open()}">
					添加
				</a-button>
			</a-row>
			<unicloud-db ref="udb" :collection="collectionList" field="s_id,type_paper_id,radio,multiple,judge,test_paper_id{_id,s_id,name}"
				:where="where" page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
				:page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error,options}"
				@load="queryload"
				:options="options" loadtime="manual">
				<uni-table ref="table" :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe
					type="selection" @selection-change="selectionChange">
					<uni-tr>
						<uni-th align="center">规则ID</uni-th>
						<uni-th align="center">试卷昵称</uni-th>
						<uni-th align="center">单选数量</uni-th>
						<uni-th align="center">多选数量</uni-th>
						<uni-th align="center">判断数量</uni-th>
						<uni-th align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data" :key="index">
						<uni-td align="center">{{item.s_id}}</uni-td>
						<uni-td align="center">{{ item.test_paper_id.length>0? item.test_paper_id[0].name :''}}({{item.test_paper_id.length>0? item.test_paper_id[0].s_id:''}})</uni-td>
						<uni-td align="center">{{item.radio}}</uni-td>
						<uni-td align="center">{{item.multiple}}</uni-td>
						<uni-td align="center">{{item.judge}}</uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button @click="setEdit(item.s_id,item._id)" class="uni-button" size="mini"
									type="primary">修改</button>
								<button @click="confirmDelete(item._id)" class="uni-button" size="mini"
									type="warn">删除</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<!-- 				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current"
						:total="pagination.count" @change="onPageChanged" />
				</view> -->
			</unicloud-db>
		</view>
	</view>
</template>

<script>
	import {
		enumConverter,
		filterToWhere
	} from '../../js_sdk/validator/role-item.js';

	const db = uniCloud.database()
	const dbCmd = db.command
	// 表查询配置
	const dbOrderBy = '' // 排序字段
	const dbSearchFields = [] // 模糊搜索字段，支持模糊搜索的字段列表。联表查询格式: 主表字段名.副表字段名，例如用户表关联角色表 role.role_name
	// 分页配置
	const pageSize = 100
	const pageCurrent = 1

	const orderByMapping = {
		"ascending": "asc",
		"descending": "desc"
	}

	export default {
		name: "list-role-item",
		props:{
			sid:""
		},
		emits:['roleAdd','roleDelete'],
		data() {
			return {
				showEditRoleItem:false,
				showAddRoleItem:false,
				idEdit:'',
				sidEdit:'',
				collectionList: "role-item,test-paper",
				query: '',
				where: '',
				orderby: dbOrderBy,
				orderByFieldName: "",
				selectedIndexs: [],
				options: {
					pageSize,
					pageCurrent,
					filterData: {},
					...enumConverter
				},
			};
		},
		created() {
			this.where = {
				s_id:dbCmd.eq(this.sid)
			}
		},
		watch:{
			sid(nv){
				console.log(nv)
				this.where = {
					s_id:dbCmd.eq(this.sid)
				}
				this.$nextTick(()=>{
					this.loadData()
				})
			}
		},
		mounted() {
			this.loadData(true)
		},
		methods: {
			queryload(data){
				let test_paper_list = [] 
				for (let s of data) {
					test_paper_list.push(s._id)
				}
				this.$emit('changeRoleItem',test_paper_list)
			},
			setEdit(sid,id){
				this.sidEdit = sid
				this.idEdit = id 
				this.showEditRoleItem = true 
				this.$refs.edit_role_item.open()
			},
			getWhere() {
				const query = this.query.trim()
				if (!query) {
					return ''
				}
				const queryRe = new RegExp(query, 'i')
				return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
			},
			loadData(clear = true) {
				this.$refs.udb.loadData({
					clear
				})
			},
			// 多选处理
			selectedItems() {
				var dataList = this.$refs.udb.dataList
				return this.selectedIndexs.map(i => dataList[i]._id)
			},
			// 批量删除
			delTable() {
				this.$refs.udb.remove(this.selectedItems(), {
					success: (res) => {
						this.$refs.table.clearSelection()
					}
				})
			},
			// 多选
			selectionChange(e) {
				this.selectedIndexs = e.detail.index
			},
			confirmDelete(id) {
				this.$refs.udb.remove(id, {
					success: (res) => {
						this.$refs.table.clearSelection()
					}
				})
			},
			sortChange(e, name) {
				this.orderByFieldName = name;
				if (e.order) {
					this.orderby = name + ' ' + orderByMapping[e.order]
				} else {
					this.orderby = ''
				}
				this.$refs.table.clearSelection()
				this.$nextTick(() => {
					this.$refs.udb.loadData()
				})
			},
			filterChange(e, name) {
				this._filter[name] = {
					type: e.filterType,
					value: e.filter
				}
				let newWhere = filterToWhere(this._filter, db.command)
				if (Object.keys(newWhere).length) {
					this.where = newWhere
				} else {
					this.where = ''
				}
				this.$nextTick(() => {
					this.$refs.udb.loadData()
				})
			}
		},

	}
</script>

<style>

</style>
